package cn.vawn.base.controller;

import cn.vawn.base.consts.AppConsts;
import cn.vawn.base.consts.EntityConsts;
import cn.vawn.base.dto.MASSubmitRsp;
import cn.vawn.base.dto.ResponseResult;
import cn.vawn.base.service.ISmsService;
import cn.vawn.base.util.MessageUtil;
import cn.vawn.base.util.PropertyUtil;
import cn.vawn.base.util.SMSUtil;
import cn.vawn.base.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("api/sms")
public class SmsController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(SmsController.class);

    @Autowired
    private ISmsService smsService;

    @Autowired
    private MessageUtil messageUtil;

    @RequestMapping(value = "/vcode/wechat/mas", method = RequestMethod.GET)
    @ResponseBody
    public ResponseResult<Object> vCodeWeChatMas(@RequestParam(value = "mobile") String mobile,
                                                 @RequestParam(value = "vCodeType") Integer vCodeType) {
        logger.info("try wechat request mas vcode by mobile: " + mobile + ", vCodeType: " + vCodeType );
        ResponseResult<Object> rr = null;
        if (StringUtil.isEmpty(mobile) || vCodeType == null) {
            rr = getCommonErrorResponse(messageUtil.getMessage(getLanguage(), "100002"));
            return rr;
        }
        String templateId = getTemplateIdByVCodeType(vCodeType);
        if (StringUtil.isEmpty(templateId)) {
            rr = new ResponseResult<>(AppConsts.RESPONSE_STATUS_ERROR, messageUtil.getMessage(getLanguage(), "200001"));
            return rr;
        }
        String vCode = SMSUtil.generateRandomVcode();
        if (StringUtil.isEmpty(vCode)) {
            rr = new ResponseResult<>(AppConsts.RESPONSE_STATUS_ERROR, messageUtil.getMessage(getLanguage(), "200006"));
            return rr;
        }
        // 微信端服务调用ip不控制
        String clientIp = "";
        // 根据安全控制策略获取处理方式(EntityConsts:验证码请求处理方式)
        int process = smsService.getVCodeProcess(mobile, clientIp, vCodeType);
        if (process == EntityConsts.V_CODE_PROCESS_BLACK) {
            rr = new ResponseResult<>(AppConsts.RESPONSE_STATUS_ERROR, messageUtil.getMessage(getLanguage(), "200003"));
        } else if (process == EntityConsts.V_CODE_PROCESS_LIMIT) {
            rr = new ResponseResult<>(AppConsts.RESPONSE_STATUS_ERROR, messageUtil.getMessage(getLanguage(), "200004"));
        } else if (process == EntityConsts.V_CODE_PROCESS_REPEAT) {
            rr = new ResponseResult<>(AppConsts.RESPONSE_STATUS_ERROR, messageUtil.getMessage(getLanguage(), "200005"));
        }
        if (process == EntityConsts.V_CODE_PROCESS_ALLOW) {
            // 发送短信请求数据
//            int validTime = PropertyUtil.getInt("sms.identity.verification.code.valid.time");
            String[] params = {vCode};
            try {
                MASSubmitRsp resultMAS = SMSUtil.sendSMSMASHttpTmpSubmit(mobile, templateId, params);
                logger.info("get vcode: " + vCode + " and response:" + resultMAS);
                if (resultMAS != null &&
                        "success".equals(resultMAS.getRspcod()) &&
                        StringUtil.notEmpty(resultMAS.getMsgGroup()) &&
                        resultMAS.getSuccess()) {
                    rr = new ResponseResult<>(AppConsts.RESPONSE_STATUS_SUCCESS, resultMAS.toString(), vCode);
                } else {
                    rr = new ResponseResult<>(AppConsts.RESPONSE_STATUS_ERROR, messageUtil.getMessage(getLanguage(), "200002"));
                }
            } catch (Exception ex) {
                rr = new ResponseResult<>(AppConsts.RESPONSE_STATUS_ERROR, messageUtil.getMessage(getLanguage(), "200002"));
            }
        }
        return rr;
    }

    /**
     * 获取验证码模板id
     * @param vCodeType
     * @return
     */
    private String getTemplateIdByVCodeType(int vCodeType) {
        String templateId = "";
        switch (vCodeType) {
            case EntityConsts.V_CODE_TYPE_ALL:
                break;
            case EntityConsts.V_CODE_TYPE_REGISTER:
                templateId = PropertyUtil.getString("sms.mas.templateId1");
                break;
            case EntityConsts.V_CODE_TYPE_LOGIN:
                templateId = PropertyUtil.getString("sms.mas.templateId1");
                break;
            case EntityConsts.V_CODE_TYPE_CHANGE_PASSWORD:
                templateId = PropertyUtil.getString("sms.mas.templateId1");
                break;
            case EntityConsts.V_CODE_TYPE_ORDER:
                break;
            case EntityConsts.V_CODE_TYPE_OTHER:
                break;
        }
        return templateId;
    }
}
